package com.earnings.okhttputils.utils.ui.activity.user;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.activity.common.StatusPriceActivity;
import com.earnings.okhttputils.utils.ui.dialog.phoenix.keybottondialog.PswKeyBoardBottomDialog;
import com.earnings.okhttputils.utils.utils.CommonUtil;

import java.util.Map;


/*
财币页面-我要直捐
 */

public class UserTreasureContributionActivity extends GodLeftHandBaseActivity implements GodOnClickListener {


    private EditText count;//金额

    @Override
    protected int onCreateLayoutId() {
        return     R.layout.activity_user_treasure_contribution;

    }

    @Override
    protected void init() {
        setTitle("我要直捐");
        showRightTextView("累计直捐");
       setOnClickListener( this,R.id.sumbit,R.id.top_right_tv,R.id.allin );
       // setText( R.id.freeze , CommonUtil.getUser().getFreeze());
        setText( R.id.info , CommonUtil.getUser().getFreeze());
        setText(R.id.name,CommonUtil.getUser().getRealname());
        setGlideImagView( R.id.head ,CommonUtil.getUser().getHead_pic(),R.mipmap.head_icon);
        count=getView( R.id.count );

        count.setHint("可捐赠财币："+CommonUtil.getUser().getWithdrawal() );
        count.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(count.getText().toString().equals("")){
                    return;}

                float nowprice=Float.parseFloat( count.getText().toString() );
                if(nowprice>Float.parseFloat(CommonUtil.getUser().getWithdrawal()))
                    count.setText( CommonUtil.getUser().getWithdrawal() );

            }


        } );
    }



    public boolean checkPrice(){
        String money=count.getText().toString();
        if(money.equals("")){
            ToastUtils.showToast(getContext(),"请输入您要捐赠的数量");
            return false;}

        return true;
    }

    @Override
    public void OnClickListener(View view) {
        int i = view.getId();
        if (i == R.id.sumbit) {
            if(checkPrice())sumbit();


        } else if (i == R.id.top_right_tv) {
            skipActivity( UserTreasureContributionRecordActivity.class );

        }else if (i == R.id.allin) {
            count.setText( CommonUtil.getUser().getWithdrawal() );
        }
    }

    public void sumbit(){
        InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(count.getWindowToken(), 0); //强制隐藏键盘
        final PswKeyBoardBottomDialog pswdialog=new PswKeyBoardBottomDialog(this,count.getText().toString());
        pswdialog.setTitle("使用招财汇宝财币支付");
        pswdialog.setOnPswKeyBoardResultListener( new PswKeyBoardBottomDialog.OnPswKeyBoardResultListener() {
            @Override
            public void getPswResult(String mResult) {

              //  showProgress("正在捐赠");
                Map map=new HttpMap( getContext() );
                map.put( "rich",count.getText().toString() );
                map.put( "paypwd",mResult);
                map.put( "operate_name","gxyxjjh");
                map.put( "operate_id","1101");
                OkHttpUtils.post().params( map ).url( HttpUrl.USER_TURN_CAIBAO_URL ).build().execute( new HttpObjectCallback<String>(getContext()) {
                    @Override
                    public void onSuccess(String object, String msg) {
                        pswdialog.showSus();
                        bundleData.putString( "title", "捐赠财币" );
                        bundleData.putString( "status", msg );
                        bundleData.putString( "info", "" );
                        bundleData.putString( "price", count.getText().toString() );
                        skipActivity( StatusPriceActivity.class );
                        finish();
                    //    dismissProgress();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pswdialog.showFail(msg);
                    //    dismissProgress();
                    }
                } );
            }
        } );

        pswdialog.show();
    }
}
